package dao

import (
	"go-demo/model"
	"gorm.io/gorm"
	"time"
)

// 创建消息
func CreateMessageSendEvent(messageSendEvent model.MessageSendEvent) *gorm.DB {
	message := model.MessageSendEvent{UserId: messageSendEvent.UserId, ToUserId: messageSendEvent.ToUserId, MsgContent: messageSendEvent.MsgContent, CreateTime: time.Now().Unix()}
	text := Db.Create(&message)
	return text
}

// 查询发送消息记录
func GetMessageSendEventByUsers(messageSendEvent model.MessageSendEvent) ([]model.MessageSendEvent, []model.MessageSendEvent) {

	var messageFromA []model.MessageSendEvent
	var messageFromB []model.MessageSendEvent

	Db.Where("from_user_id = ? and to_user_id = ?", messageSendEvent.UserId, messageSendEvent.ToUserId).Find(&messageFromA)
	Db.Where("from_user_id = ? and to_user_id = ?", messageSendEvent.ToUserId, messageSendEvent.UserId).Find(&messageFromB)

	return messageFromA, messageFromB
}

// 更新消息的状态
func UpdateMessageStatus(message model.MessageSendEvent) {
	Db.Model(&message).Update("is_viewed", true)
}
